package com.sicau.socialpracticesystem.entity.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.sicau.socialpracticesystem.common.myenum.UserTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "User对象", description = "用户表")
public class User implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "学工号")
    private Integer number;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "姓名")
    private String name;

    @ApiModelProperty(value = "身份(学生或老师)")
    private Integer identity;

    @ApiModelProperty(value = "学院id")
    private Integer collegeId;

    @ApiModelProperty(value = "班级id")
    private Integer classId;

    @ApiModelProperty(value = "年级id")
    private Integer gradeId;

    @ApiModelProperty(value = "电话")
    private String tel;

    @ApiModelProperty(value = "用户类型")
    private Integer userType;

    @ApiModelProperty(value = "盐")
    private String salt;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();

        if (this.userType == null){
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.EMPTY.getUserDes()));
            return authorities;
        }

        if (this.userType.equals(UserTypeEnum.ADMIN.getTypeCode())) {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.ADMIN.getUserDes()));
        } else if (this.userType.equals(UserTypeEnum.USER.getTypeCode())){
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.USER.getUserDes()));
        } else {
            authorities.add(new SimpleGrantedAuthority(UserTypeEnum.EMPTY.getUserDes()));
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return number.toString();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }


}
